package com.tony.blog.api.utils;

import com.upyun.FormUploader;
import com.upyun.Params;
import com.upyun.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: UpYunUtils
 * @Auther: 皮蛋布丁
 * @Date: 2021/08/22/11:12
 * @Description: 又拍云文件工具类
 */
public class UpYunUtils {

    private static final Logger log = LoggerFactory.getLogger(UpYunUtils.class);

    public static Result byteSync(byte[] datas, String filename) throws NoSuchAlgorithmException, SignatureException,
            InvalidKeyException {
        //初始化uploader
        FormUploader uploader = new FormUploader("mind-warning", "tonyzhu", "");

        //初始化参数组 Map
        final Map<String, Object> paramsMap = new HashMap<String, Object>();

        //添加 SAVE_KEY 参数
        paramsMap.put(Params.SAVE_KEY, filename);

        //添加同步上传作图参数 X_GMKERL_THUMB
        //限定图片宽度为 300px、锐化、压缩质量 80、存储为 png 格式（参数不区分先后顺序）
        paramsMap.put(Params.X_GMKERL_THUMB, "/fw/300/unsharp/true/quality/80/format/png");
        //打印结果
        Result result = null;
        result = uploader.upload(paramsMap, datas);
        log.info("上传又拍云的的结果为：{}",result);
        return result;
    }
}
